package
{
	import flash.display.MovieClip;
	import flash.display.Sprite;
	import flash.events.Event;
	
	public class Eye extends MovieClip
	{
		public static const LEFT:String = "left";
		public static const RIGHT:String = "right";
		public static const NONE:String = "none";
		//public var
		public var minking:Boolean;
		public var type:String;
		public var backColor:uint, irisColor:uint, outlineColor:uint;
		public var WIDTH:Number, HEIGHT:Number;
		// private objects
		private var back:Sprite, toplid:Sprite, bottomlid:Sprite, ball:Sprite, iris:Sprite, pupil:Sprite, speck:Sprite;
		// private var
		private var _blinking:Boolean = false;
		private var _blinkingProgress:Number = 0; // 0 to 1
		
		/** gets the current blinking action, true or false */
		public function get blinking():Boolean{return _blinking;}
		
		
		public function Eye(WIDTH:Number, HEIGHT:Number, backColor:uint, outlineColor:uint, irisColor:uint = 0xAA8866, type:String=NONE)
		{
			this.mouseEnabled = false;
			this.mouseChildren = false;
			//trace("color");
			//trace(backColor);
			//trace(backColor & 0xFEFEFE);
			//trace((backColor & 0xFEFEFE) >> 1);
			this.backColor = backColor
			this.outlineColor = outlineColor;
			this.irisColor = irisColor; 
			this.WIDTH = WIDTH;
			this.HEIGHT = HEIGHT;
			if (type == LEFT || type == RIGHT)
			{
				this.type = type;
			} else
			{
				this.type = NONE;
			}
			back = new Sprite(); 
			toplid = new Sprite(); 
			bottomlid = new Sprite();
			ball = new Sprite();
			iris = new Sprite();
			pupil = new Sprite();
			speck = new Sprite();
			
			//create order on eyes
			this.addChild(back);
			back.addChild(ball);
			ball.addChild(iris);
			ball.addChild(toplid);
			ball.addChild(bottomlid);
			iris.addChild(pupil);
			pupil.addChild(speck);
			ball.addChild(toplid);
			ball.addChild(bottomlid);
			
			drawEyeOpen();
			this.addEventListener(Event.ADDED, handleAdded);
		}
		private function handleAdded(event:Event):void
		{
			this.removeEventListener(Event.ADDED, handleAdded);
			this.addEventListener(Event.ENTER_FRAME, handleFrame);
			this.addEventListener(Event.REMOVED, handleRemoved);
		}
		/** Function draws eye fully open */
		public function drawEyeOpen():void
		{
			// back of eye
			back.graphics.clear();
			back.graphics.beginFill(outlineColor);
			back.graphics.drawEllipse(-WIDTH/2, -HEIGHT/2, WIDTH, HEIGHT);
			back.graphics.endFill();
			// white of the eye
			ball.graphics.clear();
			ball.graphics.beginFill(0xFFFFFF);
			ball.graphics.drawEllipse(-WIDTH/2+WIDTH/8, -HEIGHT/2+WIDTH/8, WIDTH-WIDTH/4, HEIGHT-WIDTH/4);
			ball.graphics.endFill();
			// color of the eye
			iris.graphics.clear();
			iris.graphics.beginFill(irisColor);
			iris.graphics.drawEllipse(-WIDTH/4, -HEIGHT/4, WIDTH/2, HEIGHT/2);
			iris.graphics.endFill();
			// black of the eye
			pupil.graphics.clear();
			pupil.graphics.beginFill(0x000000);
			pupil.graphics.drawEllipse(-WIDTH/8, -HEIGHT/8, WIDTH/4, HEIGHT/4);
			pupil.graphics.endFill();
			// draw a speck on the pupil
			speck.graphics.clear();
			speck.graphics.lineStyle(WIDTH/32,0xFFFFFF);
			speck.graphics.moveTo(-WIDTH/16,0);
			speck.graphics.lineTo(WIDTH/16,0);
			speck.graphics.moveTo(0,-WIDTH/16);
			speck.graphics.lineTo(0,WIDTH/16);
			// top lid, draw semicircle, 
			toplid.graphics.lineStyle(WIDTH/32, outlineColor);
			toplid.graphics.beginFill(backColor);
			MathUtilities.drawArc(toplid.graphics,WIDTH/16, outlineColor, 0, 0, -Math.PI/2, Math.PI/2, WIDTH/2,1);
			toplid.graphics.lineTo(-WIDTH/2, 0);
			toplid.graphics.endFill();
			toplid.scaleY = .5;
			toplid.scaleX = .8;
			toplid.y = -HEIGHT*3/8;
			// bottom lid, draw semicircle, 
			bottomlid.graphics.lineStyle(WIDTH/32, outlineColor);
			bottomlid.graphics.beginFill(backColor);
			MathUtilities.drawArc(bottomlid.graphics,WIDTH/16, outlineColor, 0, 0, Math.PI/2, -Math.PI/2, WIDTH/2,1);
			bottomlid.graphics.lineTo(WIDTH/2, 0);
			bottomlid.graphics.endFill();
			bottomlid.scaleY = .5;
			bottomlid.scaleX = .8;
			bottomlid.y = HEIGHT*3/8;
		}
		
		/** Cause eye to open and close */
		public function blink():void
		{
			_blinking = true;
			_blinkingProgress = 0;
		}
		
		private function handleFrame (event:Event):void
		{			
			// _blinking, one second
			if (_blinking)
			{
				_blinkingProgress += .04;
				if (_blinkingProgress < .5)
				{
					toplid.y += .08 * HEIGHT/4;
					bottomlid.y -= .08 * HEIGHT/4;
					toplid.height += .08*HEIGHT/4;
					bottomlid.height += .08*HEIGHT/4;
				} else if (_blinkingProgress < 1) 
				{	
					toplid.y -= .08 * HEIGHT/4;
					bottomlid.y += .08 * HEIGHT/4;
					toplid.height -= .08*HEIGHT/4;
					bottomlid.height -= .08*HEIGHT/4;
				} else if (_blinkingProgress >= 1)
				{
					_blinking = false;
				}
			}
		}
		/** remove frame listener */
		private function handleRemoved (event:Event):void
		{
			this.addEventListener(Event.ADDED, handleAdded);
			this.removeEventListener(Event.REMOVED, handleRemoved);
			this.removeEventListener(Event.ENTER_FRAME, handleFrame);
		}
	}
}